<?php
namespace App\Modules\Admin\Controllers;

use App\Http\Resources\Admin\DepartmentResource;
use App\Http\Resources\Admin\SysDepartmentOptionsResource;
use App\Modules\Admin\Services\DepartmentService;
use App\Modules\ModuleBaseController;
use Illuminate\Http\Request;

class DepartmentController extends ModuleBaseController
{
    public function index(Request $request, DepartmentService $service)
    {
        return $this->responseSuccess(DepartmentResource::collection($service->justGet($request->all())));
    }

    public function options(DepartmentService $service)
    {
        return $this->responseSuccess(SysDepartmentOptionsResource::collection($service->options()));
    }

    public function add(Request $request, DepartmentService $service)
    {
        $data = ['name', 'status', 'sort'];
        if($request->parent_id > 0) {
            $data[] = 'parent_id';
        }
        return $this->responseSuccess($service->add($request->only($data)));
    }

    public function edit(Request $request, Int $id, DepartmentService $service)
    {
        return $this->responseSuccess($service->edit($id, $request->all()));
    }

    public function detail(Int $id, DepartmentService $service)
    {
        return $this->responseSuccess($service->detail($id));

    }

    public function del(Int|String $id, DepartmentService $service)
    {
        return $this->responseSuccess($service->del($id));
    }
}

